<?php

/**
 * This is the model class for table "news".
 *
 * The followings are the available columns in table 'news':
 * @property integer $news_id
 * @property string $title
 * @property string $description
 * @property integer $order
 * @property integer $taxonomy_id
 * @property string $seo_title
 * @property string $seo_keywork
 * @property string $seo_description
 * @property string $image
 * @property string $short_description
 */
class News extends CActiveRecord {

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'news';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('order, taxonomy_id', 'numerical', 'integerOnly' => true),
            array('title, seo_title, seo_keywork, seo_description, image', 'length', 'max' => 255),
            array('description,created,short_description', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('news_id, title, description, order, taxonomy_id, seo_title, seo_keywork, seo_description, image', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'taxonomy' => array(self::BELONGS_TO, 'Taxonomy', 'taxonomy_id'),
            'banners' => array(self::HAS_MANY, 'Banner', 'object'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'news_id' => 'News',
            'title' => 'Title',
            'description' => 'Description',
            'order' => 'Order',
            'taxonomy_id' => 'Taxonomy',
            'seo_title' => 'Seo Title',
            'seo_keywork' => 'Seo Keywork',
            'seo_description' => 'Seo Description',
            'image' => 'Image',
            'short_description' => 'giới thiệu ngắn'
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('news_id', $this->news_id);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('order', $this->order);
        $criteria->compare('taxonomy_id', $this->taxonomy_id);
        $criteria->compare('seo_title', $this->seo_title, true);
        $criteria->compare('seo_keywork', $this->seo_keywork, true);
        $criteria->compare('seo_description', $this->seo_description, true);
        $criteria->compare('image', $this->image, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return News the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * get image of Taxonomy
     */
    public function getImage(){
        $default = "default.gif";
        if($this->image != null && file_exists(PATH_IMAGE_NEWS.$this->image) && strlen($this->image) > 0){
            return $this->image;
        }else{
            if ($this->news_id != null) {
                return $default;
            }
        }
        return false;
    }
    
    /**
     * 
     */
    public static function getNewNews($limit = 5){
        return self::model()->findAll('taxonomy_id <> '.ROOT_CATEGORY,array('order' => 'created DESC','limit' => $limit));
    }
    
    /**
     * 
     */
    public function getShortDescription($length = 100){
        if(strlen($this->short_description) > $length){
             $return = substr($this->short_description, 0,$length);
             if(($position = strrpos($return," ")) !== false){
                $return  =  substr($return, 0,$position);
             }
             return $return;
        }
        else
            return $this->short_description;
    }
    
    /**
     * 
     */
    public function getUrl(){
       return  Yii::app()->createUrl('news/view',array('title' => getTitleUrl($this->title),'id' => $this->news_id));
    }
    
    /**
     * 
     */
    public function registerMeta(){
       $keyword = $this->seo_keywork != null?$this->seo_keywork:$this->title;
       $description = $this->seo_description != null?$this->seo_description:$this->title;
       Yii::app()->clientScript->registerMetaTag($keyword,'keywords');
       Yii::app()->clientScript->registerMetaTag($description,'description');
    }
    /**
     * 
     */
    public function getSeoTitle(){
        if($this->seo_title == null){
            return $this->title;
        }
        return $this->seo_title;
    }
    
    
    

}
